import tkinter as tk
import tkinter.ttk as ttk
import socket,threading
a=1
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def leave():
    global s,text
    text.delete('0.0', tk.END)
    s.send('离开聊天室'.encode('utf-8'))
    s.close()
def shou(s, msg_text):
    recv_data = s.recv(1024).decode('utf-8')
    msg_text.insert(tk.END, recv_data)
    while 1:
        try:
            recv_data = s.recv(1024).decode('utf-8')
            msg_text.insert(tk.END, recv_data)
            ck=recv_data.split(':')
            print(ck)
            if ck[0] not in '\n公告':
                ls.insert(tk.END,ck[0])
        except:
            msg_text.delete('0.0',tk.END)
            msg_text.insert(tk.END,'您已离开聊天室，聊天中断')
            break

def fa():
    global a,inp,text,s
    try:
        if a == 0:
            msg = inp.get('0.0', tk.END)
            s.send(msg.encode('utf-8'))
            inp.delete('0.0', tk.END)
        else:
            msgname = inp.get('0.0', tk.END)
            s.send(msgname.encode('utf-8'))
            inp.delete('0.0', tk.END)
            text.delete('0.0', tk.END)
            a = 0
    except:
        text.delete('0.0',tk.END)
        text.insert(tk.END,'提示：您没有进入聊天室，发送无效')
def connt():
    global s,text
    adr=en1.get()
    duan=int(en2.get())
    s.connect((f'{adr}',duan))
    print('向服务器端发送请求...等待回应....')
    t1 = threading.Thread(target=shou, args=(s,text))
    t1.daemon = True
    t1.start()
root=tk.Tk()
root.geometry("600x500+400+300")
root.title('创维局域网聊天客户端v2.0')
su=tk.LabelFrame(root,text='聊天设置',font='微软雅黑 10',pady=5)
su.pack(fill=tk.X,padx=5,pady=5)
con=tk.Button(su,text='连接申请',padx=5,bg='lightskyblue',command=connt)
con.pack(side=tk.LEFT,pady=2,padx=10)
la=tk.Label(su,text='   主机端IP',font='微软雅黑 10')
la.pack(side=tk.LEFT,padx=5)
en1=tk.Entry(su)
en1.pack(side=tk.LEFT)
en1.insert(0,'192.168.1.3')

la1=tk.Label(su,text='       端口',font='微软雅黑 10')
la1.pack(side=tk.LEFT,padx=5)
en2=tk.Entry(su)
en2.pack(side=tk.LEFT)
en2.insert(0,666)

f1=tk.Frame(root)
f1.pack(fill=tk.BOTH)
#------------聊天内容框----------------
text=tk.Text(f1,width=50,height=15,font='微软雅黑 10')
text.grid(row=0,column=0,padx=5,pady=7)
#------------列表框----------------
ls=tk.Listbox(f1,width=20,height=14,font='微软雅黑 10',bg='beige')
ls.grid(row=0,column=1,padx=5,pady=7)
ls.insert(tk.END,' ------\n聊天成员列表\n------')
#------------分隔线----------------
se=ttk.Separator(root,orient=tk.HORIZONTAL)
se.pack(fill=tk.X)
f2=tk.Frame(root)
f2.pack(fill=tk.BOTH)
#------------聊天输入框----------------
inp=tk.Text(f2,width=50,height=5,font='微软雅黑 10')
inp.grid(row=0,column=0,rowspan=2,padx=5,pady=7)
#------------控制按钮----------------
bu2=tk.Button(f2,text='离 开',font='微软雅黑 12',bg='lightskyblue',
             width=12,padx=5,pady=3,command=leave)
bu2.grid(row=0,column=1,padx=15,pady=5)
bu=tk.Button(f2,text='发 送',font='微软雅黑 12',bg='lightskyblue',
             width=12,padx=5,pady=3,command=fa)
bu.grid(row=1,column=1,padx=15,pady=5)

root.mainloop()
